<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class block extends a_base {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_block');
    }

    public function index() {
        $orderkey_list = array('blockid', 'blockname','clicknum', 'listid', 'adminid', 'addtime', 'isclose');
        $orderkey = $this->input->get('orderkey');
        if (!in_array($orderkey, $orderkey_list)) {
            $orderkey = 'blockid';
        }
        $ascdesc = $this->input->get('ascdesc');
        if ($ascdesc != 'asc') {
            $ascdesc = 'desc';
        }
        $where = array();
        $blockname = $this->input->get('blockname');
        if (isset($_GET['blockname']) && !empty($blockname)) {
            $where['blockname'] = $blockname;
        }
        
        $blockcontent= $this->input->get('blockcontent');
        if (isset($_GET['blockcontent']) && !empty($blockcontent)) {
            $where['blockcontent'] = $blockcontent;
        }
       
        $isclose = $this->input->get('isclose');
        if (isset($_GET['isclose']) && $isclose != -1) {
            $where['isclose'] = $isclose;
        }
        
        $page = $this->input->get('page'); //当前页
        $page = intval($page);
        if ($page < 1) {
            $page = 1;
        }
        $cnt = $this->m_block->get_count($where);
       
        $page_size = 10;
        $arr = array(
            'page' => $page, //当前页
            'page_size' => $page_size, //分页总大小
            'total' => $cnt//总记录数
        );
        $start_page = ($page - 1) * $page_size;
        $data['pagination'] = new pagination($arr);

        $t_data = array(
            'where' => $where,
            'limit' => $page_size,
            'offset' => $start_page,
            'order' => $orderkey . ' ' . $ascdesc,
        );
        $data['list']= $this->m_block->get_block_list($t_data);
      

        $this->load->model('m_admin');
        $data['admin'] = $this->m_admin->get_all(array('column' => 'adminname,adminid'));
 
        

        $where['orderkey'] = $orderkey;
        $where['ascdesc'] = $ascdesc;
        $data['where'] = $where;
        $this->load->view('block/index', $data);
    }

   
    public function add() {
        $defalut = array(
            'blockname' => '',
            'blockcontent' => '',
            'clicknum' => 1,
            'listid' => 1,
            'isclose' => 0
        );
        $blockid = $this->input->get('blockid');
        if ($blockid > 0) {
            //修改
            $block = $this->m_block->get_block(array('blockid' => $blockid));
            if (empty($block)) {
                // 提示
                $vt_data = array(
                    'js_fun' => 'tips',
                    'param' => array('片段不存在', 'index'),
                );
                $this->load->view('tips', $vt_data);
                return;
            }
            $data['post_url'] = b_url('block/add?blockid=' . $blockid);
            $data['block'] = $block;
        } else {
            $data['block'] = $defalut;
            $data['post_url'] = b_url('block/add');
        }
        if (isset($_POST['btnSave'])) {
            $post = $this->input->post();
            $result = array();
            foreach ($post as $k => $v) {
                if (isset($defalut[$k])) {
                    $defalut[$k] = $post[$k];
                }
            }
	    $defalut['blockcontent'] = $_REQUEST['blockcontent'];
            if ($blockid > 0) {
                $defalut['blockid'] = $defalut['blockid'];
                $this->m_block->update($defalut);
            } else {
                $defalut['adminid'] = $this->session->userdata['user_id'];
                $defalut['addtime'] = date('Y-m-d H:i:s');
                $this->m_block->insert($defalut);
            }
            redirect('block/index');
            exit;
        }
       
        $this->load->view('block/add', $data);
    }

    function delete() {
        $blockid = $this->input->get('blockid');
        $list = explode('.', $blockid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_block->delete(array('blockid' => $v));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $blockid) . '片段删除成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }

    function openclose() {
        $blockid = $this->input->get('blockid');
        $status = $this->input->get('status');
        $str_status = '关闭';
        if ($status != 1) {
            $status = 0;
            $str_status = '开放';
        }
        $list = explode('.', $blockid);
        foreach ($list as $k => $v) {
            $v = intval($v);
            if ($v > 0) {
                $this->m_block->update(array('blockid' => $v, 'isclose' => $status));
            }
        }
        // 提示
        $vt_data = array(
            'js_fun' => 'tips',
            'param' => array('编号为' . str_replace('.', '，', $blockid) . '片段' . $str_status . '成功!', 'index', 1),
        );
        $this->load->view('tips', $vt_data);
    }
 

}
